package cn.antia.mail;

import org.apache.commons.mail.HtmlEmail;

import cn.antia.cep.busi.DbHibernateService;
import cn.antia.cep.common.AntiaLog;
import cn.antia.cep.common.ContextBean;
import cn.antia.cep.common.DBFlag;
import cn.antia.cep.db.hibernate.bean.*;

import java.util.*;

import javax.mail.internet.InternetAddress;

public class MailUtil {
	private static HtmlEmail email;
	private static ResourceBundle rb = ResourceBundle.getBundle("cn.antia.cep.mail");
	
	public static HtmlEmail getHtmlEmail(){
		if(email == null){
			email = new HtmlEmail();
			email.setCharset(rb.getString("charset"));
			email.setHostName(rb.getString("stmp"));
			email.setAuthentication(rb.getString("account"),rb.getString("password"));
		}
		return email;
	}
	
	public static void sendEmail(String to, String subject, String content){
		HtmlEmail email = getHtmlEmail();
		try{
			email.setFrom(rb.getString("from"));
			List list = new ArrayList();
			if(to.indexOf(";") > 0){
				String[] tos = to.split(";");
				for(int i = 0;i<tos.length;i++){
					list.add(new InternetAddress(tos[i]));
				}
			}else{
				list.add(new InternetAddress(to));
			}
			email.setSubject(subject);
			email.setTo(list);
			email.setHtmlMsg(content);
			email.send();
		}catch(Exception e){
			AntiaLog.error("Send email fail ,message:" + e.getMessage());
			EmailFail fail = new EmailFail();
			fail.setContent(content);
			fail.setSubject(subject);
			fail.setTos(to);
			fail.setFailTime(new Date());
			fail.setValid(new Integer(DBFlag.VALID));
			DbHibernateService service = (DbHibernateService)ContextBean.getBean("dbHibernateService");
			service.create(fail);
		}
	}
	
	private static void sendFailEmail(EmailFail emailFail,DbHibernateService service){
		HtmlEmail email = getHtmlEmail();
		try{
			List list = new ArrayList();
			email.setFrom(rb.getString("from"));
			String to = emailFail.getTos();
			if(to.indexOf(";") > 0){
				String[] tos = to.split(";");
				for(int i = 0;i<tos.length;i++){
					list.add(new InternetAddress(tos[i]));
					email.addTo(tos[i]);
				}
			}else{
				list.add(new InternetAddress(to));
			}
			email.setTo(list);
			email.setSubject(emailFail.getSubject());
			email.setHtmlMsg(emailFail.getContent());
			email.send();
			emailFail.setValid(new Integer(DBFlag.INVALID));
			service.update(emailFail);
		}catch(Exception e){
			AntiaLog.error("Send fail email fail ,message:" + e.getMessage());
		}
	}
	
	
	
	public static void sendFail(){
		DbHibernateService service = (DbHibernateService)ContextBean.getBean("dbHibernateService");
		String hql = "from EmailFail where valid = " + DBFlag.VALID;
		List list = service.find(hql);
		if(list != null && list.size() > 0){
			EmailFail fail;
			for(int i = 0;i<list.size();i++){
				fail = (EmailFail)list.get(i);
				sendFailEmail(fail, service);
			}
		}
	}
	/*
	public static void main(String[] args){
		sendEmail("xiaobin2021@163.com", "测试", "你好啊，你好啊！！<h2>好！</h2>");
		System.out.println("success.");
	}*/
}
